package com.xxg.renrensearch.service;

import com.alibaba.fastjson.JSON;
import com.xxg.renrensearch.cache.ShopCache;
import com.xxg.renrensearch.dao.Product;
import com.xxg.renrensearch.dao.RenrenShopShop;
import com.xxg.renrensearch.domain.query.GetShopListQuery;
import com.xxg.renrensearch.domain.query.SearchShopListQuery;
import com.xxg.renrensearch.domain.result.*;
import com.xxg.renrensearch.es.RenRenShopEs;
import com.xxg.renrensearch.repository.RenrenShopShopRepository;
import com.xxg.renrensearch.service.common.CommonService;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商户商品接口实现
 *
 * @author xuxiaogang
 * @date 2021-11-17
 */
@Service
public class ShopServiceImpl implements ShopService {
    private final ShopCache shopCache;
    private final RenrenShopShopRepository renrenShopShopRepository;
    private final RenRenShopEs renRenShopEs;
    private final CommonService commonService;

    public ShopServiceImpl(ShopCache shopCache, RenrenShopShopRepository renrenShopShopRepository,
                           RenRenShopEs renRenShopEs, CommonService commonService) {
        this.shopCache = shopCache;
        this.renrenShopShopRepository = renrenShopShopRepository;
        this.renRenShopEs = renRenShopEs;
        this.commonService = commonService;
    }

    @Override
    public BaseResult<GetShopResult> getShopProduct(GetShopListQuery query) {
        // 首先查询缓存
        Object result = shopCache.queryCache(query.getPageNo(), query.getCityCode());
        // 缓存不存在情况处理
        if (result == null) {
            Pageable pageable = PageRequest.of(query.getPageNo() - 1, 10);
            Page<RenrenShopShop> data;
            if (query.getCityCode() == 0) {
                // 搜索全国
                data = renrenShopShopRepository.findAll(pageable);
            } else {
                data = renrenShopShopRepository.myFindByCityCode(query.getCityCode(), pageable);
            }
            GetShopResult shopResult = new GetShopResult(new ArrayList<>(data.getContent().size()),
                    data.getTotalElements(), data.getTotalPages(), data.getNumber() + 1);
            // 根据商铺查询其商品信息
            Pageable pageable2 = PageRequest.of(0, 3);
            String[] requiredFields = {"id", "skuName", "spuThumb", "skuPrice"};
            data.getContent().forEach(e -> {
                List<Product> goods = renRenShopEs.findByShopId(e.getId(), pageable2, requiredFields,
                        "skuPrice", SortOrder.ASC, null);
                shopResult.getContent().add(new ShopWithProducts(e, goods));
            });
            // 写入缓存
            shopCache.writeCache(query.getPageNo(), query.getCityCode(), shopResult);
            commonService.extraInfoProcess(query.getShopId(), shopResult.getContent());
            return new BaseResult<>(shopResult);
        }
        GetShopResult shopResult = JSON.parseObject(result.toString(), GetShopResult.class);
        commonService.extraInfoProcess(query.getShopId(), shopResult.getContent());
        return new BaseResult<>(shopResult);
    }

    @Override
    public BaseResult<GetShopResult> getShop(SearchShopListQuery query) {
        // 首先查询缓存
        Object result = shopCache.queryTimeCache(query.getPageNo(), query.getCityCode(), query.getKeyword());
        // 缓存不存在情况处理
        if (result == null) {
            Pageable pageable = PageRequest.of(query.getPageNo() - 1, 10);
            Page<RenrenShopShop> data;
            if (query.getCityCode() == 0) {
                data = renrenShopShopRepository.findByName(query.getKeyword(), pageable);
            } else {
                data = renrenShopShopRepository.myFindByNameOrCityCode(query.getKeyword(), query.getCityCode(), pageable);
            }
            GetShopResult shopResult = new GetShopResult(new ArrayList<>(data.getContent().size()),
                    data.getTotalElements(), data.getTotalPages(), data.getNumber() + 1);
            data.getContent().forEach(e -> shopResult.getContent().add(new ShopWithProducts(e, null)));
            // 写入缓存
            shopCache.writeTimeCache(query.getPageNo(), query.getCityCode(), query.getKeyword(), shopResult);
            commonService.extraInfoProcess(query.getShopId(), shopResult.getContent());
            return new BaseResult<>(shopResult);
        }
        GetShopResult shopResult = JSON.parseObject(result.toString(), GetShopResult.class);
        commonService.extraInfoProcess(query.getShopId(), shopResult.getContent());
        return new BaseResult<>(shopResult);
    }
}
